﻿    using LJX.SmartPark.Common.Extensions;
using LJX.SmartPark.Common.ResponseOutput;
using LJX.SmartPark.Domain.Area.Monthcard;
using LJX.SmartPark.Service.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LJX.SmartPark.Service.Area
{
    public class MonthcardServeice : IMonthcardServeice
    {
        private readonly IRepository<Monthcard> _monthcardServeice;

        public MonthcardServeice(IRepository<Monthcard> monthcardServeice)
        {
            _monthcardServeice = monthcardServeice;
        }

        /// <summary>
        /// 获取总条数
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public int GetCount()
        {
            return _monthcardServeice.GetCount();
        }

        /// <summary>
        /// 显示分页
        /// </summary>
        /// <returns></returns>
        public async Task<Response<List<Monthcard>>> GetMonthcardAsync(FYMonthcard fYMonthcard)
        {
            var monthcard = await _monthcardServeice.GetAllListAsync();
            var result = monthcard.MapToList<Monthcard>();
            if (!string.IsNullOrEmpty(fYMonthcard.number))
            {
                result = result.Where(a => a.Number.Contains(fYMonthcard.number)).ToList();
            }
            if (!string.IsNullOrEmpty(fYMonthcard.plate))
            {
                result = result.Where(a => a.Plate.Contains(fYMonthcard.plate)).ToList();
            }
            if (!string.IsNullOrEmpty(fYMonthcard.type))
            {
                result = result.Where(x => x.Type == int.Parse(fYMonthcard.type)).ToList();
            }
            if (!string.IsNullOrEmpty(fYMonthcard.state))
            {
                result = result.Where(x => x.State == int.Parse(fYMonthcard.state)).ToList();
            }
            var totalCount = result.Count();
            result = result.Skip((fYMonthcard.pageIndex - 1) * fYMonthcard.pageSize).Take(fYMonthcard.pageSize).ToList();
            return Response<List<Monthcard>>.Ok(result, totalCount);
            //var query = from a in monthcard
            //            join b in monthcard on a.Id equals b.Id
            //            select new
            //            {
            //                a.Id,
            //                a.Number,
            //                a.Name,
            //                a.phone,
            //                a.Plate,
            //                a.Begindate,
            //                a.Expiredate,
            //                a.Type,
            //                a.Stall,
            //                a.State
            //            };
            //query = query.WhereIf(!string.IsNullOrEmpty(fY.number), x => x.Number.Contains(fY.number.Trim()));
            ////query = query.WhereIf(fY.plate > 0, x => x.Plate == fY.plate);
            //var totalCount = query.Count();
            //query=query.Skip((fY.pageIndex-1)*fY.pageSize).Take(fY.pageSize);
            //var result = query.MapToList<Monthcard>();
            //return Response<List<Monthcard>>.Ok(result, totalCount);
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public Response<Monthcard> GetMonthcardById(Monthcard input)
        {
            var monthcard = _monthcardServeice.GetListQuery().FirstOrDefault(m => m.Id == input.Id);
            var result = monthcard.MapTo<Monthcard>();
            return Response<Monthcard>.Ok(result);
        }

        /// <summary>
        /// 分页
        /// </summary>
        public class FYMonthcard
        {
            public int pageIndex { get; set; }
            public int pageSize { get; set; }

            public string? number { get; set; }

            public string? plate { get; set; }

            public string? type { get; set; }

            public string? state { get; set; }
        }
    }
}
